<?php
class DatePicker extends CWidget
{
	public $id;
	public $cssClass = 'dpcontroller2';
	public $startTime;
	public $endTime;
	// 可选值 array('today', 'yesterday', 'lastweek', 'recent', 'thismonth', 'lastmonth', 'thirtydays', 'custom');
	public $typeArr = array('today', 'yesterday', 'lastweek', 'recent', 'thismonth', 'lastmonth', 'thirtydays', 'custom');
	public $dateType;
	public $dateString;
	
	public $dataName = 'dpSelectData';
	
	private $_langArr;
	
	public function run()
	{
		$this->_langArr = array(
			'today'		=> 'today',
			'yesterday'	=> 'yesterday',
			'recent'	=> 'last seven days',
			'lastweek'	=> 'last week',
			'thismonth'	=> 'this month',
			'lastmonth'	=> 'last month',
			'thirtydays'=> 'last thirty days',
			'custom'	=> 'custom time'
		);
		
		$this->initDateInfo();
		$this->render('datePicker');
	}
	
	public function getSelectData()
	{
		$array = array();
		foreach ($this->typeArr as $value) {
			$array[$value] = Yii::t('lang', $this->_langArr[$value]);
		}
		return json_encode($array);
	}
	
	public function initDateInfo()
	{
		foreach ($this->typeArr as $type) {
			if ($type !== 'custom' && method_exists(timeOper, $type)) {
				$arr = timeOper::$type();
				if ($this->_isEqual($arr['startTime'], $arr['endTime'])) {
					$this->dateType = $type;
					break;
				}
			}
		}
		$this->dateString = $this->dateType ? Yii::t('lang', $this->_langArr[$this->dateType]) : 
					date('Y-m-d', $this->startTime) .'/'.date('Y-m-d', $this->endTime);
		$this->dateType || $this->dateType = 'custom';
	}
	
	private function _isEqual($startTime, $endTime) {
		return $this->startTime == $startTime && ($this->endTime == $endTime || $this->endTime == $endTime+86399);
	}
}
?>